package com.healthyeating.manage.service.impl;

import java.util.List;

import com.healthyeating.common.exception.ServiceException;
import com.healthyeating.common.utils.SecurityUtils;
import com.healthyeating.manage.mapper.FoodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.healthyeating.manage.mapper.FoodTypeMapper;
import com.healthyeating.manage.domain.FoodType;
import com.healthyeating.manage.service.IFoodTypeService;

/**
 * 食材类型Service业务层处理
 * 
 * @author 段奇乐
 * @date 2024-12-02
 */
@Service
public class FoodTypeServiceImpl implements IFoodTypeService 
{
    @Autowired
    private FoodTypeMapper foodTypeMapper;

    @Autowired
    private FoodMapper foodMapper;

    /**
     * 查询食材类型
     * 
     * @param foodTypeId 食材类型主键
     * @return 食材类型
     */
    @Override
    public FoodType selectFoodTypeByFoodTypeId(Long foodTypeId)
    {
        return foodTypeMapper.selectFoodTypeByFoodTypeId(foodTypeId);
    }

    /**
     * 查询食材类型列表
     * 
     * @param foodType 食材类型
     * @return 食材类型
     */
    @Override
    public List<FoodType> selectFoodTypeList(FoodType foodType)
    {
        foodType.setUserId(SecurityUtils.getUserId());
        return foodTypeMapper.selectFoodTypeList(foodType);
    }

    /**
     * 新增食材类型
     * 
     * @param foodType 食材类型
     * @return 结果
     */
    @Override
    public int insertFoodType(FoodType foodType)
    {
        if (foodType.getParentId() == null) {
            foodType.setParentId(0L);
        }
        foodType.setIsEnabled(0);
        foodType.setUserId(SecurityUtils.getUserId());
        return foodTypeMapper.insertFoodType(foodType);
    }

    /**
     * 修改食材类型
     * 
     * @param foodType 食材类型
     * @return 结果
     */
    @Override
    public int updateFoodType(FoodType foodType)
    {
        return foodTypeMapper.updateFoodType(foodType);
    }

    /**
     * 批量删除食材类型
     * 
     * @param foodTypeIds 需要删除的食材类型主键
     * @return 结果
     */
    @Override
    public int deleteFoodTypeByFoodTypeIds(Long[] foodTypeIds)
    {
        Long foodTypeId = foodTypeIds[0];
        FoodType foodType = foodTypeMapper.selectFoodTypeByFoodTypeId(foodTypeId);
        if (foodType != null){
            if (foodType.getIsEnabled() == 1){
                throw new ServiceException("该类型已启用，请禁用后删除");
            }
            // 查询该类型下是否存在子类型
            List<FoodType> foodTypeChildren = foodTypeMapper.selectFoodTypeByParentId(foodTypeId);
            if (!foodTypeChildren.isEmpty()) {
                throw new ServiceException("该类型下存在子类型，无法删除");
            }
            // 查询是否有关联的食材
            List<FoodType> foodList = foodMapper.selectFoodListByFoodTypeId(foodTypeId);
            if (!foodList.isEmpty()) {
                throw new ServiceException("该类型下存在关联食材，无法删除");
            }
        }
        return foodTypeMapper.deleteFoodTypeByFoodTypeIds(foodTypeIds);
    }

    /**
     * 删除食材类型信息
     * 
     * @param foodTypeId 食材类型主键
     * @return 结果
     */
    @Override
    public int deleteFoodTypeByFoodTypeId(Long foodTypeId)
    {
        return foodTypeMapper.deleteFoodTypeByFoodTypeId(foodTypeId);
    }
}
